<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="运单编号" prop="transportNo">
        <el-input v-model="queryParams.transportNo" placeholder="请输入运单编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="承运商" prop="carrier">
        <el-select v-model="queryParams.carrier" placeholder="请选择承运商" clearable>
          <el-option v-for="dict in dict.type.aid_carrier_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.aid_transport_status" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['aid:transport:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['aid:transport:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['aid:transport:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['aid:transport:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="transportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="运单编号" align="center" prop="transportNo"/>
      <el-table-column label="物资分配" align="center" prop="allocationTitle"/>
      <el-table-column label="承运商" align="center" prop="carrier">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.aid_carrier_type" :value="scope.row.carrier"/>
        </template>
      </el-table-column>
      <el-table-column label="司机姓名" align="center" prop="driverName"/>
      <el-table-column label="联系电话" align="center" prop="driverPhone"/>
      <el-table-column label="车牌号" align="center" prop="plateNumber"/>
      <el-table-column label="发货地址" align="center" prop="fromAddress" :show-overflow-tooltip="true"/>
      <el-table-column label="收货地址" align="center" prop="toAddress" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.aid_transport_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['aid:transport:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['aid:transport:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-truck" @click="handleDelivery(scope.row)" v-if="scope.row.status === '0'" v-hasPermi="['aid:transport:delivery']">发货</el-button>
          <el-button size="mini" type="text" icon="el-icon-check" @click="handleComplete(scope.row)" v-if="scope.row.status === '1'" v-hasPermi="['aid:transport:complete']">完成</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改物流运输对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="物资分配" prop="allocationId">
              <el-select v-model="form.allocationId" placeholder="请选择物资分配">
                <el-option
                  v-for="item in allocationOptions"
                  :key="item.allocationId"
                  :label="item.allocationTitle"
                  :value="item.allocationId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="承运商" prop="carrier">
              <el-select v-model="form.carrier" placeholder="请选择承运商">
                <el-option
                  v-for="dict in dict.type.aid_carrier_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="司机姓名" prop="driverName">
              <el-input v-model="form.driverName" placeholder="请输入司机姓名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="driverPhone">
              <el-input v-model="form.driverPhone" placeholder="请输入联系电话"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="车牌号" prop="plateNumber">
              <el-input v-model="form.plateNumber" placeholder="请输入车牌号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预计时间" prop="estimatedTime">
              <el-date-picker
                v-model="form.estimatedTime"
                type="datetime"
                placeholder="请选择预计到达时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="发货地址" prop="fromAddress">
          <el-input v-model="form.fromAddress" type="textarea" placeholder="请输入发货地址"/>
        </el-form-item>
        <el-form-item label="收货地址" prop="toAddress">
          <el-input v-model="form.toAddress" type="textarea" placeholder="请输入收货地址"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTransport, getTransport, delTransport, addTransport, updateTransport, deliveryTransport, completeTransport } from "@/api/aid/transport";
import { listAllocation } from "@/api/aid/allocation";

export default {
  name: "Transport",
  dicts: ['aid_carrier_type', 'aid_transport_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物流运输表格数据
      transportList: [],
      // 物资分配选项
      allocationOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        transportNo: null,
        carrier: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        allocationId: [
          { required: true, message: "物资分配不能为空", trigger: "change" }
        ],
        carrier: [
          { required: true, message: "承运商不能为空", trigger: "change" }
        ],
        driverName: [
          { required: true, message: "司机姓名不能为空", trigger: "blur" }
        ],
        driverPhone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        plateNumber: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        fromAddress: [
          { required: true, message: "发货地址不能为空", trigger: "blur" }
        ],
        toAddress: [
          { required: true, message: "收货地址不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getAllocationOptions();
  },
  methods: {
    /** 查询物流运输列表 */
    getList() {
      this.loading = true;
      listTransport(this.queryParams).then(response => {
        this.transportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询物资分配选项 */
    getAllocationOptions() {
      listAllocation().then(response => {
        this.allocationOptions = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        transportId: null,
        transportNo: null,
        allocationId: null,
        carrier: null,
        driverName: null,
        driverPhone: null,
        plateNumber: null,
        estimatedTime: null,
        fromAddress: null,
        toAddress: null,
        status: '0',
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物流运输";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const transportId = row.transportId || this.ids;
      getTransport(transportId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物流运输";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.transportId != null) {
            updateTransport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTransport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const transportIds = row.transportId || this.ids;
      this.$modal.confirm('是否确认删除物流运输编号为"' + transportIds + '"的数据项？').then(() => {
        return delTransport(transportIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 发货按钮操作 */
    handleDelivery(row) {
      const transport = { transportId: row.transportId, status: '1' };
      deliveryTransport(transport).then(response => {
        this.$modal.msgSuccess("发货成功");
        this.getList();
      });
    },
    /** 完成按钮操作 */
    handleComplete(row) {
      const transport = { transportId: row.transportId, status: '2' };
      completeTransport(transport).then(response => {
        this.$modal.msgSuccess("运输完成");
        this.getList();
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('aid/transport/export', {
        ...this.queryParams
      }, `transport_${new Date().getTime()}.xlsx`);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.transportId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    }
  }
};
</script> 